package com.infolab.aims.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.infolab.aims.common.PaginationHelper;
import com.infolab.aims.common.QueryHelper;
import com.infolab.aims.common.bean.SearchCriteria;
import com.infolab.aims.common.bean.SearchResult;
import com.infolab.aims.domain.SystemAccessRight;
import com.infolab.aims.domain.SystemRole;
import com.infolab.aims.service.intf.RoleService;


@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	
	Log logger = LogFactory.getLog(getClass());
	
	@PersistenceContext
	private EntityManager em;
	
	public void createRole(SystemRole systemRole)
	{
		systemRole.setDateCreated(new Date());
		
		systemRole.setActive(true);
		
		SystemRole newSystemRole = new SystemRole();
		
		systemRole.setModifiedBy(systemRole.getCreatedBy());
		systemRole.setDateCreated(new Date());
		systemRole.setDateModified(new Date());
				
		BeanUtils.copyProperties(systemRole,newSystemRole);
		
		em.persist(newSystemRole);
		
		systemRole.setSystemRoleId(newSystemRole.getSystemRoleId());
		
	}

	public void updateRole(SystemRole systemRole) {
		
		systemRole.setDateModified(new Date());
		
		if(systemRole.getSection()==null || systemRole.getSection().getSectionId()==null)
			systemRole.setSection(null);
		
		SystemRole curSystemRole = em.find(SystemRole.class, systemRole.getSystemRoleId());
		List<SystemAccessRight> systemAccessRight = systemRole.getSystemAccessRights();
		List<SystemAccessRight> newSystemAccessRights = new ArrayList<SystemAccessRight>();
		
		curSystemRole.setRoleName(systemRole.getRoleName());
		curSystemRole.setSection(systemRole.getSection());
		curSystemRole.setModifiedBy(systemRole.getModifiedBy());
		curSystemRole.setDateModified(systemRole.getDateModified());
		
		if(systemAccessRight!=null)
		{
			for(int i=0;i<systemAccessRight.size();i++)
			{
				SystemAccessRight tmp = systemAccessRight.get(i);
				String tmpKey = tmp.getAccessRightId();
				tmp = em.getReference(SystemAccessRight.class, tmpKey);
				newSystemAccessRights.add(tmp);
			}
		}
		curSystemRole.setSystemAccessRights(newSystemAccessRights);
		
		em.merge(curSystemRole);
	}
	


	public void deleteRole(SystemRole systemRole) {		
		systemRole.setDateModified(new Date());
		systemRole.setActive(false);
		em.merge(systemRole);
		
	}


	public SystemRole getRole(Integer systemRoleId) {
		
		return em.find(SystemRole.class, systemRoleId);
	}

	public SystemRole findRole(Map<String, Object> searchCriteria) {
		// TODO Auto-generated method stub
		return null;
	}

	public SearchResult searchRole(
			SearchCriteria searchCriteria) {
		
		if(searchCriteria.getCurPage()==null)
			searchCriteria.setCurPage(1);
		
		SystemRolePaginationHelper helper = new SystemRolePaginationHelper();
		
		return helper.getSearchResultFromQueryHelper(em, searchCriteria);
	}
	
	public static class SystemRolePaginationHelper extends PaginationHelper<SystemRole> {

		@Override
		public String[] getValidColumnName() {
			return new String[]{"roleName","active"};
		}
		
		@Override
		public String[] getValidSortName() {
			return new String[]{"roleName","section.sectionName","modifiedBy","dateModified"};
		}

		@Override
		public String getDefaultSortName() {
			return "roleName";
		}
		
		@Override
		public QueryHelper getQueryHelper() {
			QueryHelper queryHelper = new QueryHelper();
			queryHelper.select("select systemRole from SystemRole systemRole");
			return queryHelper;
		}
	}

}
